<?php
namespace Frieza\Modules\Decorationtask\Controllers;

use function Clue\StreamFilter\fun;
use Illuminate\Support\MessageBag;
use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use App\Admin\Extensions\Tools\UserButton;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Form;
use Encore\Admin\Widgets\Tab;
use Illuminate\Http\Request;
use Doraemon\model\DecorationTask\BudgetPrompt;

class BudgetController extends Controller
{
    const PAGE_SIZE = 20;

    public function __construct()
    {
        parent::__construct();
        Admin::script('$(".pjax-container").attr("id","pjax-container");');
    }

    //预算列表
    public function index(Request $request,Content $content){
        $request = $request->all();

        $tab = new Tab();
        $tab->addLink("预算提示信息列表" ,'/admin/decorationtask/budget_prompt');
        $tab->addLink('添加预算提示信息', '/admin/decorationtask/budget_prompt/create');

        $list = BudgetPrompt::getBudgetPrompt([]);

        return $content
            ->header('装修预算')
            ->breadcrumb(
                ['text' => '任务列表', 'url' => '/decorationtask/admintask/budget_prompt/index']
            )
            ->body($tab)->row(self::listGrid($request));
    }


   //添加或者修改预算表
    public function edit(Request $request,Content $content ,$id = 0)
    {
        $request = $request->all();
        $tab = new Tab();
        $tab->addLink("预算提示信息列表" ,'/admin/decorationtask/budget_prompt');
        if ($id == 0) {
            $tab->addLink('添加预算提示信息', '/admin/decorationtask/budget_prompt/create' , true);
            return $content
                ->header('装修预算')
                ->breadcrumb(
                    ['text' => '任务列表', 'url' => '/decorationtask/admintask/budget_prompt/index']
                )
                ->body($tab)->row(self::editAction($request, $id));
        } else {
            $tab->addLink('编辑预算提示信息', '/admin/decorationtask/budget_prompt//budget_prompt/{id}/edit' , true);
            return $content
                ->header('装修预算')
                ->breadcrumb(
                    ['text' => '任务列表', 'url' => '/decorationtask/admintask/budget_prompt/index']
                )
                ->body($tab)->row(self::editAction($request, $id));
        }
    }

    //修改列表
    public  function editAction($request = []  , $id = 0)
    {
        if($id == 0){
            $data = [];
        }else{
           $data =  BudgetPrompt::getBudgetInfoById($id);
        }
        $form = new Form($data);
        if($id > 0){
            $form->hidden('id');
        }
        $form->text('remark', '提示文案')->setWidth(4, 2)->required('不能为空')->help('20字以内');
        $form->number('start_percent', '开始百分比')->setWidth(4, 2)->default(0)->help("<span style='color:red'> 精度为小数点后两位 ，代表 大于等于 </span>");
        $form->number('end_percent', '结束百分比')->setWidth(4, 2)->default(0)->help("<span style='color:red'>精度为小数点后两位 ，代表 小于等于</span>");
        $form->color('info_color', '颜色')->setWidth(4, 2)->required('不能为空');
        $form->action('/admin/decorationtask/budget_prompt/save');
        return $form->render();
    }

    //
    public function listGrid($request = []){
        $condition = [];
        if($request['percent']['start'] && $request['percent']['end']){
            $condition['start_percent'] = $request['percent']['start'];
            $condition['end_percent'] = $request['percent']['end'];
        }
        $data = BudgetPrompt::getBudgetPrompt([] , $condition);
        $gridModel = new pGridDataModel($data,count($data), [
            'perPage' => $request['per_page'] ? : $request['LIMIT'][1]
        ]);

        $listGrid = Admin::grid($gridModel, function (Grid $grid) {
            $grid->disableCreateButton();
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->disableIdFilter();
                $filter->between('percent', '百分比范围')->condition($_GET);
            });
            $grid->disablePagination();
            $grid->column('id', 'id');
            $grid->column('remark', '提示文案');
            $grid->column('info_color', '背景颜色')->display(function(){
                return '<p style="background-color: '.$this->info_color.';color:'.$this->info_color.';width: 50px; display: block">颜色</p>';
            });
            $grid->column('end_percent')->hide();
            $grid->column('start_percent')->hide();
            $grid->column('range_percent',"百分比范围")->display(function () {
                return $this->start_percent . '%-' . $this->end_percent."%";
            });
            $grid->column('admin_id', '操作人')->display(function(){
                if(!$this->admin_id) {
                    return '暂无';
                }else{
                    return Admin::user()->find($this->admin_id)->name;
                }
            });
            $grid->column('edittime', '编辑时间')->display(function(){
                if($this->edittime == '1970-01-01 00:00:00'){
                    return '';
                }else{
                    return $this->edittime;
                }
            });
            $grid->column('addtime', '创建时间');
        });
        $list = $listGrid->render();
        return $list;
    }

    //提交修改
    public function editSubmit(Request $request)
    {
        $request = $request->all();
        $data = [
            'remark' => $request['remark'],
            'info_color' => $request['info_color'],
            'start_percent' => $request['start_percent'],
            'end_percent' => $request['end_percent'],
            'admin_id' => Admin::user()->id,
            'info_status'=>BudgetPrompt::STATUS_NORMAL
        ];

        if(isset($request['id']) ) {
            $data['edittime'] = date('Y-m-d H:i:s', time());
            $id = $request['id'];

            $rs = BudgetPrompt::edit($data, $id);
        } else {
            $data['addtime'] = date('Y-m-d H:i:s', time());
            $rs = BudgetPrompt::add($data);
        }


        if($rs) {
            if(isset($request['id'])) {
                $title = '修改成功';
            } else {
                $title = '添加成功';
            }
            $success = new MessageBag([
                'title' => $title
            ]);
            return redirect('/admin/decorationtask/budget_prompt')->with(compact('success'));
        } else {
            if(isset($request['id'])) {
                $title = '修改失败';
            } else {
                $title = '添加失败';
            }
            $error = new MessageBag([
                'title' => $title
            ]);
            return redirect('/admin/decorationtask/budget_prompt')->with(compact('error'));
        }

    }


    public function del( $id = 0)
    {
        $info = BudgetPrompt::getBudgetInfoById($id);
        if(empty($info)) {
            return response()->json([
                'status' => false,
                'message' => "内容不存在",
            ]);
        }
        //新建关系，删除旧关系
        $data = [
            'info_status' => BudgetPrompt::STATUS_DELETE,
            'edittime' => date('Y-m-d H:i:s', time()),
            'admin_id' => Admin::user()->id
        ];

        $rs = BudgetPrompt::edit($data,$id);
        if($rs) {
            return response()->json([
                'status' => true,
                'message' => "操作成功",
            ]);
        } else {
            return response()->json([
                'status' => false,
                'message' => "操作失败",
            ]);
        }

    }



}